﻿using System.Collections.Generic;
using System.Windows.Forms;
using Mbs.Globalization;
using Mbs.Elements;
using Mbs.ComponentModel.Container;

namespace Mbs.Windows.Elements.Builders
{
    [Component(Id = "ToolStrip.LanguageMenuItem", Service = typeof(IElementBuilder))]
    public class LanguageMenuItemBuilder:ToolStripMenuItemBuilder
    {
        protected override void OnInitInstance(object caller, Mbs.Modularity.AddIns.IAddInItem element, List<object> subItems, System.Windows.Forms.ToolStripItem instance)
        {
            var menuItem = instance as ToolStripMenuItem;
            if (menuItem != null)
            {
                //if (element.IsSite())
                    ElementService.Sites.RegisterSite(element.Path, menuItem);

                foreach (var o in subItems)
                    if (o is ToolStripItem)
                        ElementService.Sites[element.Path].Add(o, null);

                SetToolStripItemOwner(menuItem, element);
                menuItem.Click += (s, e) => LanguageManager.Instance.Language = element.Properties.Get<string>("Language", LanguageManager.Instance.Language);
            }
        }
    }
}
